<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="proCode">
        <el-input
          v-model="queryParams.proCode"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品名称" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="修改时间">
        <el-date-picker
          v-model="daterangeModifyDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" width="70" align="left">
        <template slot-scope="scope">
          <el-radio
            class="radio"
            :label="scope.$index+1"
            v-model="selectGoodsRow"
          >&emsp;&emsp;&emsp;
          </el-radio>
        </template>
        </el-table-column>
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="商品ID" align="center" prop="id"/>
      <el-table-column label="商品编号" :show-overflow-tooltip="true" align="center" prop="proCode" />
      <el-table-column label="商品名称" align="center" prop="proName" />
      <el-table-column label="商品单位" align="center" prop="unitName" />
      <el-table-column label="商品类别" align="center" prop="typeName" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods, checkProCodeRequest } from "@/api/basedata/goods";
import { listAllType } from "@/api/basedata/type";
import { v4 as uuidv4 } from 'uuid';

export default {
  name: "GoodsIndex",
  props:['goodsArrIndex'],
  data() {
      const checkProCode = (rule, value, callback) => {
      const queryParams = {
        proCode: value
      };
      checkProCodeRequest(queryParams).then(rs => {
          if (rs.code == "200") {
            callback();
          } else {
            callback(new Error('商品编码已存在'));
          }
        })
    };

    return {
      selectGoodsRow:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 商品备注时间范围
      daterangeCreateDate: [],
      // 商品备注时间范围
      daterangeModifyDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proCode: null,
        proName: null,
        typeId: null,
        createDate: null,
        modifyDate: null,
      },
      unitList: [
        { id: 1, unitName: "个" },
        { id: 2, unitName: "包" },
        { id: 3, unitName: "盒" },
      ],
      typeList: [
        { id: 1, typeName: "食品" },
        { id: 2, typeName: "衣服" },
        { id: 3, typeName: "鞋子" },
        { id: 4, typeName: "化妆品" },
        { id: 5, typeName: "其他" },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        proCode: [
          { required: true, message: "商品编号不能为空", trigger: "blur" },
          { validator:checkProCode,message: "商品编码已存在", trigger: 'blur' }
        ],
        proName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        unitId: [
          { required: true, message: "商品单位不能为空", trigger: "change" }
        ],
        typeId: [
          { required: true, message: "商品类别不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  watch:{
    selectGoodsRow: {
      handler(newVal, oldVal) {
        console.log(newVal)
        //使用 $emit派发事件
        // console.log('aaaa',this.goodsArrIndex)
        //  第一个参数  是 商品对象  第二个参数 对应 商品数组的索引
        this.$emit("selectGoodsMethod",this.goodsList[newVal-1],this.goodsArrIndex);
      }
    }
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateDate && '' != this.daterangeCreateDate) {
        this.queryParams.params["beginCreateDate"] = this.daterangeCreateDate[0];
        this.queryParams.params["endCreateDate"] = this.daterangeCreateDate[1];
      }
      if (null != this.daterangeModifyDate && '' != this.daterangeModifyDate) {
        this.queryParams.params["beginModifyDate"] = this.daterangeModifyDate[0];
        this.queryParams.params["endModifyDate"] = this.daterangeModifyDate[1];
      }
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取所有商品分类 */
    getTypeAll() {
      listAllType().then(response => {
        this.typeList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        proCode: null,
        proName: null,
        unitId: null,
        price: null,
        typeId: null,
        proRemark: null,
        createBy: null,
        createDate: null,
        modifyBy: null,
        modifyDate: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateDate = [];
      this.daterangeModifyDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

  }
};
</script>
